package com.huakang.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.huakang.entity.Account;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Author{小熊}
 * @Date2025/1/3 15:49
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        //System.out.println("LoginInterceptor.preHandle");
        HttpSession session = request.getSession();
        Account account = (Account)session.getAttribute("loginAccount");

        if(account==null){
            ObjectMapper objectMapper = new ObjectMapper();
            String s = objectMapper.writeValueAsString(ResultVo.noAuth("您还未登陆！"));
            response.getWriter().write(s);
            return false;
        }
        return true;
    }
}
